
import java.text.DecimalFormat;

public class Complex {
    double realPart;
    double imagePart;

    public Complex() {

    }

    public Complex(double r, double i) {
        realPart = r;
        imagePart = i;
    }

    public void set(double r, double i) {
        realPart = r;
        imagePart = i;
    }

    public double getRealPart() {
        return realPart;
    }

    public double getImagePart() {
        return imagePart;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String s = "";
        if (getRealPart() == 0 && getImagePart() == 0) {
            s = "0";
        } else if (getRealPart() != 0 && getImagePart() == 0) {
            s = s + getRealPart();
        } else if (getRealPart() == 0 && getImagePart() != 0) {
            s = s + getImagePart() + "i";
        } else {
            if (getImagePart() < 0) {
                s = s + getRealPart() + getImagePart() + "i";
            } else {
                s = s + getRealPart() + "+" + getImagePart() + "i";
            }

        }
        return s;
    }

    public Complex complexAdd(Complex a) {
        return new Complex((getRealPart() + a.getRealPart()), (getImagePart() + a.getImagePart()));
    }

    public Complex complexSub(Complex a) {
        return new Complex((getRealPart() - a.getRealPart()), (getImagePart() - a.getImagePart()));
    }

    public Complex complexMulti(Complex a) {
        double imaginePart = getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() - getImagePart() * a.getImagePart();
        return new Complex(realPart, imaginePart);
    }

    public Complex complexDiv(Complex a) throws Exception {
        double imaginePart = -getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() + getImagePart() * a.getImagePart();
        double div = a.getRealPart() * a.getRealPart() + a.getImagePart() * a.getImagePart();
        if (div == 0) {
            throw new Exception();
        }
        realPart = realPart / div;
        imaginePart = imaginePart / div;
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        return new Complex(Double.valueOf(decimalFormat.format(realPart)), Double.valueOf(decimalFormat.format(imaginePart)));
    }
}